package com.grq.mis.service;

import com.grq.mis.dao.CertTypeDao;
import com.grq.mis.entity.CertType;
import com.grq.mis.exception.BusinessException;
import com.grq.mis.exception.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Transactional
@Service
public class CertTypeService extends BaseService<CertType>{

    @Autowired
    private CertTypeDao certTypeDao;

    public void addCertType(CertType certType) {
        certType.setIsDisable(false);
        certType.setCreateTime(new Date());
        certTypeDao.insert(certType);
    }

    public void updateCertType(CertType model) {

        CertType certType = certTypeDao.single(model.getId());
        if (certType == null) {
            throw new BusinessException(ResultEnum.CERT_TYPE_IS_NULL);
        }

        certType.setName(model.getName());
        certType.setDescription(model.getDescription());
        certType.setIsDisable(model.getIsDisable());
        certTypeDao.updateById(certType);

    }

    public void deleteCertType(Long id) {
        CertType certType = certTypeDao.single(id);
        if (certType == null) {
            throw new BusinessException(ResultEnum.CERT_TYPE_IS_NULL);
        }

        certTypeDao.deleteById(id);
    }
}
